package cn.dyp.com.netconnection.adapter;

import java.util.List;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import cn.dyp.com.netconnection.R;
import cn.dyp.com.netconnection.bean.SensorDataBean;

/**
 * 自定义适配器类
 * 
 * @author asus
 */

public class DataAdapter extends ArrayAdapter<SensorDataBean> {
	private Context mContext;
	private List<SensorDataBean> mList;
	private LayoutInflater mInflater;

	/**
	 * 视图封装类
	 * 
	 * @author asus
	 *
	 */
	private class ViewHolder {
		TextView mCo2TV;// co2文本框
		TextView mLightTV;// 光照文本框
		TextView mAirTTV;// 空气温度文本框
		TextView mAirHTV;// 空气湿度文本框
		TextView mS0ilTTV;// 土壤温度文本框
		TextView mSoilHTV;// 土壤湿度文本框
	}

	/**
	 * 构造函数
	 * 
	 * @param context
	 *            上下文对象
	 * @param objects
	 *            数据源
	 */
	public DataAdapter(Context context, List<SensorDataBean> objects) {
		super(context, 0, objects);
		this.mContext = context;
		this.mList = objects;
		this.mInflater = (LayoutInflater) mContext
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	/**
	 * 获取去数据源长度
	 * 
	 * @see ArrayAdapter#getCount()
	 */
	@Override
	public int getCount() {
		return mList.size();
	}

	/**
	 * 获取单个数据源
	 * 
	 * @see ArrayAdapter#getItem(int)
	 */
	@Override
	public SensorDataBean getItem(int position) {
		return mList.get(position);
	}

	/**
	 * 获取自定义视图
	 * 
	 * @see ArrayAdapter#getView(int, View,
	 *      ViewGroup)
	 */
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder mHolder = null;
		SensorDataBean mBean = mList.get(position);
		// 初始化控件
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.data_item, parent, false);
			mHolder = new ViewHolder();
			mHolder.mCo2TV = (TextView) convertView.findViewById(R.id.co2_tv);
			mHolder.mLightTV = (TextView) convertView
					.findViewById(R.id.light_tv);
			mHolder.mAirTTV = (TextView) convertView
					.findViewById(R.id.airt_t_tv);
			mHolder.mAirHTV = (TextView) convertView
					.findViewById(R.id.air_h_tv);
			mHolder.mS0ilTTV = (TextView) convertView
					.findViewById(R.id.soil_t_tv);
			mHolder.mSoilHTV = (TextView) convertView
					.findViewById(R.id.soil_h_tv);
			convertView.setTag(mHolder);
		} else {
			mHolder = (ViewHolder) convertView.getTag();
		}
		// 给控件设定数值
		if (mBean != null) {
			mHolder.mCo2TV.setText(String.valueOf(mBean.getCo2()));
			mHolder.mLightTV.setText(String.valueOf(mBean.getLight()));
			mHolder.mAirTTV.setText(String.valueOf(mBean.getAirT()));
			mHolder.mAirHTV.setText(String.valueOf(mBean.getAirH()));
			mHolder.mS0ilTTV.setText(String.valueOf(mBean.getSoilT()));
			mHolder.mSoilHTV.setText(String.valueOf(mBean.getSoilH()));
		}
		return convertView;
	}
}
